
(in-package :tutorial)

(defun get-all-milestones-for-me ()
  (let ( (l (db.agraph.user::show-ms)))
  (mapcar #'(lambda (it) 
              (setf (fourth it) (subseq (fourth it) 0 10))
              ) l )
    l))

(publish :path "/list-all-milestones"
    :content-type "text/html"
    :function 
    #'(lambda (req ent)
         (with-http-response (req ent)
           (with-http-body (req ent)
             (let ( (the-triples (cons '("ID" "Name" "Owner" "Creation Date" ) (get-all-milestones-for-me) )))
               (html 
                  (:html (:head (:title "Milestones"))
                         (:body 
                          (:ul (:li "Not to be confused with millstones")
                               (:li "Though they can feel like that sometimes"  )
                               (:li "there are " (:princ-safe (- (length the-triples) 1)) " milestones")
                               (:li ((:a :href "home") "Home") :br)
                               )
                                    :p :hr :p
                          
                          
                          (make-table the-triples) :p :hr
                          
                         
                         ); end body
                         )) ;end html
               ) ; end let
             ))))



(publish :path "/enter-new-milestone"
    :content-type "text/html"
    :function 
         #'(lambda (req ent)
             (let* (
                    ; ( the-id (text-request "ID" req) )
                    ( the-name (text-request "name" req) )
                    ( the-description (text-request "description" req))
                    ; ( the-creation-date (text-request "creation-date" req))
                    ( the-comment (text-request "comment" req))
                    ( the-link (text-request "link" req))
                    ; ( the-owner (text-request "owner" req))
                    ( the-target-date (text-request "target-date" req))
                    ( the-predecessor (text-request "predecessor" req))
                    
                      ; ( f (int-request "m-frequency" req) )
                      )

         (with-http-response (req ent)
           (with-http-body (req ent)
              (let ((the-actor-list (get-all-actors-for-me) ))
               (html 
                  (:html (:head (:title "Enter New Milestone"))
                         (:body 
                          (:ul (:li "Not to be confused with millstones")
                               (:li "Though they can feel like that sometimes"  )
                               (:li ((:a :href "home") "Home") :br)
                               )
                          :p :hr :p
                          ((:form :action "create-new-milestone")
                           (:table
                            ; (:tr (:td "name") (:td   ((:input :type "text" :name "name" :value the-name :maxlength "80"))))
                            ; (nice-text-input "ID" the-id)
                            (nice-text-input "name" the-name)
                            (nice-text-input "description" the-description)
                            ; (nice-text-input "creation-date" the-creation-date)
                            (nice-text-input "comment" the-comment)
                            (nice-text-input "link" the-link)
                            ; (nice-text-input "owner" the-owner)
                            (nice-text-input "target-date" the-target-date)
                            (nice-text-input "predecessor" the-predecessor)
                            (two-item-table-row "predecessor"
                                                (nice-list-box-with-values "ppredecessor" 
                                                                           (mapcar #'second (get-all-milestones-for-me))))
                            (two-item-table-row "owner" 
                                                (nice-list-box-with-values "owner" the-actor-list))
                            (two-item-table-row :hr :hr)
                            (two-item-table-row " " (submit-button))
                            ) ; end table
                           ) ; end form
                         ); end body
                         )) ;end html
                ) ; end let
             )))))

(publish :path "/create-new-milestone"
    :content-type "text/html"
    :function 
         #'(lambda (req ent)
             (let* (
                    ( the-name (text-request "name" req) )
                    ( the-description (text-request "description" req))
                    ( the-creation-date (get-current-date-in-the-appropriate-format))
                    ( the-comment (text-request "comment" req))
                    ( the-link (text-request "link" req))
                    ( the-owner (text-request "owner" req))
                    ( the-target-date (text-request "target-date" req))
                    ( the-predecessor (text-request "predecessor" req))
                    ( the-id (commit-milestone  the-name the-description the-creation-date the-comment the-link the-owner the-target-date the-predecessor) )
                    )
         (with-http-response (req ent)
           (with-http-body (req ent)
             ; (let ((count 0 ))
               (html 
                  (:html (:head (:title "New Milestone"))
                         (:body 
                          (:ul (:li "Here is the new milestone you just created :-)")
                               (:li "..."  )
                           (:li ((:a :href "list-all-milestones") "List All Milestones") :br)
                               (:li ((:a :href "home") "Home") :br)
                               )
                          :p :hr :p
                          
                          ( make-table (list 
                                        (list "ID" the-id)
                                        (list "name" the-name)
                                        (list "description" the-description)
                                        (list "creation-date" the-creation-date)
                                        (list "comment" the-comment)
                                        (list "link" the-link)
                                        (list "owner" the-owner)
                                        (list "target-date" the-target-date)
                                        (list "predecessor" the-predecessor)
                                        ))))))))))
                          
                          

(defun commit-milestone ( the-name the-description the-creation-date the-comment the-link the-owner the-target-date the-predecessor)
  (let* (
        ( the-resource-as-list (list ; 'res 'mile-stone
                                     ; 'id the-id
                                     'name the-name
                                     'description the-description
                                     'creation-date the-creation-date
                                     'comment the-comment
                                     'link the-link
                                     'owner the-owner
                                     'target-date the-target-date
                                     'predecessor the-predecessor
                                ))
         (the-id (id-from-list the-resource-as-list) )
        )
    
    (gen-res::commit-resource-as-list-02 'mile-stone the-id the-resource-as-list)
    the-id
    ))

